import { polarPoints } from '../../algorithm/polar.js'
import { createPathFn } from "../path2.js"
import { plainMatrix } from "../../math/index.js"


export const _peanoPoints = (options) => {

    let { o = [400, 300], r = 100, a = 0, depth = 3 } = options
    let n = 4
    let points = polarPoints({
        o, r, a,
        n,
    })


    let _iter = (points, { depth, a }) => {
        // let stack = []
        let ps = points.map((t, i) => {
            let m = i % 4
            let a2 = a
            let direction = 1
            if (m === 0) {
                direction = -1
                a2 = a + 90
            } else if (m === 3) {
                a2 = a - 90
                direction = -1
            }

            return polarPoints({
                o: t,
                r: r / Math.pow(2, (depth + 1))
                , a: a2
                ,
                n,
                direction
            })
            // if (i === 0) {
            //     stack.push(t)
            // }
            // let next = points[i + 1]
            // if (next) {
            //     stack.push(..._item(t, next))

            // }
        })
        return plainMatrix(ps)

    }

    if (depth > 0) {
        let ps = points
        for (let i = 0; i < depth; i++) {
            ps = _iter(ps, {
                depth: i,
                a
            })
        }
        return ps
    }
    return points
}


export const peanoPoints = (options) => {

    let _iter = (options, { depth }) => {
        if (depth === 0) {
            let { o = [400, 300], r = 100, a = 0 } = options
            let n = 4
            let points = polarPoints({
                o, r, a,
                n
            })
            return points

        } else if (depth == 1) {

            let points = _iter(options, { depth: 0 })
            let d = Math.pow(4, depth - 1)
            let { a, r } = options
            let ps = points.map((t, i) => {
                let a2 = a
                let r2 = r / Math.pow(2, depth)
                let m = parseInt(i / d)
                if (m === 0) {
                    a2 = a + 90
                    return _iter({
                        ...options,
                        o: t,
                        r: r2,
                        a: a2,
                    }, { depth: 0 }).reverse()
                }
                else if (m === 3) {
                    a2 = a - 90
                    return _iter({
                        ...options,
                        o: t,
                        r: r2,
                        a: a2,
                    }, { depth: 0 }).reverse()
                }


                return _iter({
                    ...options,
                    o: t,
                    r: r2,
                    a: a2,
                }, { depth: 0 })
            })

            return plainMatrix(ps)


        } else {
            let points = _iter(options, { depth: depth - 1 })

            let d = Math.pow(4, depth - 1)
            let { a, r } = options
            let ps = points.map((t, i) => {
                let a2 = a
                let r2 = r / Math.pow(2, depth)
                let m = parseInt(i / d)
                let o = t
                if (m === 0) {
                    a2 = a + 90
                    return _iter({
                        ...options,
                        o,
                        r: r2,
                        a: a2,
                    }, { depth: 0 }).reverse()
                }
                else if (m === 3) {
                    a2 = a - 90
                    return _iter({
                        ...options,
                        o,
                        r: r2,
                        a: a2,
                    }, { depth: 0 }).reverse()
                }


                return _iter({
                    ...options,
                    o,
                    r: r2,
                    a: a2,
                }, { depth: 0 })
            })
            return plainMatrix(ps)
        }
    }
    let { depth } = options
    let ps = _iter(options, { depth })
    return ps
    // let d = Math.pow(4, depth)
    // return ps.slice(0,d )

}

export const _peano = createPathFn(peanoPoints)
